<template>
  <div class="nav-container">
    <div class="container">
      <div class="nav">
        <div class="categories-wrapper">
          <div class="category-btn" @click="toggleCategories">
            <i class="menu-icon"></i>
            所有地州
          </div>
          <transition name="slide">
            <div class="categories-menu" v-show="showCategories">
              <div class="category-item" v-for="item in categories" :key="item.id" @click="gotoRegion(item)">
                {{ item.name }}
                <!--                <i class="arrow-right" v-if="category === '健康'"></i>-->
              </div>
            </div>
          </transition>
        </div>

        <nav class="main-nav">
          <div
              v-for="(item, index) in navItems"
              :key="index"
              class="nav-item"
              @click="goto(item)"
          >
            {{ item }}
          </div>
        </nav>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Navigation',
  data() {
    return {
      showCategories: false,
      categories: [],
      navItems: ['全部商品', '充值送现金', '我的积分', '关于']
    }
  },
  mounted() {
    this.getRegion()
  },
  methods: {
    goto(item) {
      if (item === '全部商品') {
        this.showCategories = false
        this.$router.push({path: "/allPro", query: {keyword: 'all'}})
      } else if (item === '充值送现金') {
        this.$router.push({path: "/UserInfo/balance"})
      } else if (item === '我的积分') {
        this.$router.push({path: "/UserInfo/Integration"})
      }
    },
    toggleCategories() {
      this.showCategories = !this.showCategories
    },
    getRegion() {
      this.categories = []
      this.$api.home.getRegions().then(res => {
        if (res.code == 200) {
          res.data.forEach(item => {
            this.categories.push({
              id: item.id,
              name: item.name
            })
          })
        }
      })
    },
    gotoRegion(item) {
      this.$router.push({path: "/allPro", query: {keyword: "brand", Id: item.id}})
    }
  }
}
</script>

<style scoped>
.nav-container {
  /*display: flex;*/
  background-color: #f5f5f5;
  /*position: relative;*/
}

.nav {
  position: relative;
  display: flex;
}

.categories-wrapper {
  position: relative;
}

.category-btn {
  display: flex;
  align-items: center;
  background-color: #8AB660;
  color: white;
  padding: 15px 25px;
  cursor: pointer;
  font-size: 14px;
  user-select: none;
}

.menu-icon {
  width: 18px;
  height: 2px;
  background-color: white;
  position: relative;
  margin-right: 10px;
}

.menu-icon::before,
.menu-icon::after {
  content: '';
  position: absolute;
  width: 18px;
  height: 2px;
  background-color: white;
  left: 0;
}

.menu-icon::before {
  top: -6px;
}

.menu-icon::after {
  top: 6px;
}

.categories-menu {
  position: absolute;
  top: 100%;
  left: 0;
  width: 200px;
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  overflow: auto;
  max-height: 300px;
}

.category-item {
  padding: 12px 20px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background-color 0.3s;
}

.category-item:hover {
  background-color: #f5f5f5;
}

.arrow-right {
  border: solid #999;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(-45deg);
}

.main-nav {
  display: flex;
  padding: 0 20px;
}

.nav-item {
  padding: 15px 20px;
  color: #333;
  cursor: pointer;
  display: flex;
  align-items: center;
  font-size: 14px;
  position: relative;
}

.arrow-down {
  border: solid #333;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(45deg);
  margin-left: 5px;
}

/* 过渡动画 */
.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.slide-enter,
.slide-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
  }

  .main-nav {
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  .categories-menu {
    width: 100%;
  }
}
</style>